package com.xzj.admin.resource.analysis.handle.analysis.cmdb;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzj.admin.analysis.base.constants.CommonEnums;
import com.xzj.admin.analysis.base.domain.CommonFlowInfo;
import com.xzj.admin.analysis.base.domain.ResMessageManage;
import com.xzj.admin.resource.analysis.cache.BaseCache;
import com.xzj.admin.resource.analysis.mapper.ResMessageManageMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @program: xzj-admin
 * @ClassName: SourceToCmdbHandle
 * @description: 消息源解析为cmdb对象
 * @author: XiaZunJun
 * @create: 2024-11-21 09:15
 */
@Component
@Slf4j
public class SourceToCmdbHandle {

    @Resource
    private ResMessageManageMapper resMessageManageMapper;

    /**
     * 循环处理云资源池下的所有资源主题
     * @author XiaZunJun
     * @date 2024/11/21 上午9:17
     * @param info 通用流转对象
     *
     */
    public void foreachRmPoolTheme(CommonFlowInfo info){
        Object[] sortThemeKey = BaseCache.getSortThemeKey();
        ResMessageManage queryInfo = new ResMessageManage();
        queryInfo.setMessageTag(CommonEnums.MESSAGE_TAG.pushed.name());
        queryInfo.setCloudId(info.getCloudId());
        queryInfo.setStandardCode(info.getStandardCode());
        queryInfo.setRmPoolId(info.getRmPoolId());
        for (Object key : sortThemeKey) {
            String theme = BaseCache.getTheme((Integer)key);
            messagePagePush(info, queryInfo,theme);
        }
    }

    private PageInfo<ResMessageManage> queryExecuteMessagePage(int pageNum, ResMessageManage queryInfo){
        PageHelper.startPage(pageNum,2000);
        List<ResMessageManage> collectMiddleMessages =
                resMessageManageMapper.queryExecuteMessage(queryInfo);
        return new PageInfo<>(collectMiddleMessages);
    }

    private void messagePagePush(CommonFlowInfo info, ResMessageManage queryInfo,String subTheme) {
        queryInfo.setMessageSource(subTheme);
        PageInfo<ResMessageManage> pageInfo = queryExecuteMessagePage(1, queryInfo);
        long total = pageInfo.getTotal();
        log.info("{}-{}资源推送cmdb-{}开始 ", info.getRmPoolId(), subTheme, total);
        // 判断是否走入计划任务判断
        // judgeCmdbTask(info,pageInfo.getList(), subTheme);
        log.info("{}-{} 资源推送cmdb-{}结束 ", info.getRmPoolId(), subTheme, total);
        if(total>2000){
            messagePagePush(info,queryInfo,subTheme);
        }else{
            pushSubMessage(info, queryInfo, subTheme);
        }
    }

    /**
     * 判断是否有同步推送的子主题
     *
     * @param queryInfo 查询推送数据的基础对象
     * @param theme     判断子主题的父级主题
     * @author xiazunjun
     * @date 2023/8/16 16:07
     */
    private void pushSubMessage(CommonFlowInfo info, ResMessageManage queryInfo, String theme) {
        // 判断是否存在子集
        List<String> subThemes = BaseCache.getSubThemes(theme);
        // 循环处理子集
        for (String subTheme : subThemes) {
            messagePagePush(info, queryInfo,subTheme);
        }
    }
}
